#Region "Import Section"
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Text
Imports System.IO
Imports System.Configuration
Imports MyClasses
Imports DAL
Imports Excel
#End Region

Public Class frmMasterPreview

#Region "Declaration"
    'Public strFilePath As String
    Public strPrevSheet As String
    'Public dsRecord1 As DataSet
    Public dsPrevRecord As DataSet
    Public ByteClick As Byte
    'Dim dsRecord As DataSet
    'Dim ExcelFilePath As String = ""

    ''Dim MyConnection As OleDbConnection
    ''Dim MyAdapter As OleDbDataAdapter
    
#End Region

#Region "Form Control Events"
    Private Sub frmMasterPreview_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            BCommon.SetFormStandards(Me)
            If ByteClick = 1 Then
                Me.Text = "Data Preview for " & strPrevSheet.ToString() & " Sheet"
            Else
                Me.Text = "Log Report for " & strPrevSheet.ToString() & " Sheet"
            End If

            Call ShowPreview()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmMasterPreview_Load")
        End Try
    End Sub
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            Me.Close()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
        End Try
    End Sub
#End Region

#Region "User Defined Function"
    'Read the Excel Data from the specified path
    Private Sub ShowPreview()
        Dim intCount As Integer = 0
        Dim intCol As Integer = 0
        Try

            fgDataPreview.DataSource = Nothing
            'ExcelFilePath = Trim(strFilePath)   '//Excel file path
            'MyConnection = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=" & ExcelFilePath & "; Extended Properties=Excel 8.0 ; ")
            'Dim query = "SELECT * FROM [" & strPrevSheet & "$]"
            'MyAdapter = New OleDbDataAdapter(query, MyConnection)
            'MyAdapter.TableMappings.Add("Table", "ExcelSheet")
            'dsRecord = New DataSet
            'MyAdapter.Fill(dsRecord)

            With fgDataPreview
                'Remove Previous Row from the Grid
                .Rows.RemoveRange(0, .Rows.Count - 1)
                If dsPrevRecord.Tables(0).Rows.Count > 16 Then
                    .Rows.Count = dsPrevRecord.Tables(0).Rows.Count + 1
                ElseIf dsPrevRecord.Tables(0).Rows.Count = 16 Then
                    .Rows.Count = 18
                Else
                    .Rows.Count = 17
                End If

                'Set the New Column in the grid according to new Sheet
                .Cols.Count = dsPrevRecord.Tables(0).Columns.Count + 1
                For intCol = 1 To dsPrevRecord.Tables(0).Columns.Count
                    'Set the column name in the grid
                    .Item(0, intCol) = dsPrevRecord.Tables(0).Columns(intCol - 1).ColumnName.Replace("#", ".")
                    'MsgBox(dsPrevRecord.Tables(0).Rows.Item(0).Item(intCol).ToString())
                    'Set the column width in the grid
                    '.Cols(intCol).Width = .Item(0, intCol).ToString().Length * 15
                Next
                'Call SetColumnWidth(strPrevSheet)
                'Set the New Record in the grid according to new Sheet 
                For intCount = 0 To dsPrevRecord.Tables(0).Rows.Count - 1
                    If IsNothing(dsPrevRecord.Tables(0).Rows(intCount)) = False Then
                        For intCol = 1 To dsPrevRecord.Tables(0).Columns.Count
                            If IsNothing(dsPrevRecord.Tables(0).Rows(intCount).Item(intCol - 1)) = False Then
                                .Item(intCount + 1, intCol) = dsPrevRecord.Tables(0).Rows(intCount).Item(intCol - 1)
                            End If
                        Next
                    End If
                Next

                .Cols(1).Width = .Width / 12 * 4         'Item Name  
                .Cols(2).Width = .Width / 20 * 4         'Short Name
                If strPrevSheet <> "OpeningStock" Then .Cols(3).Width = .Width / 12 * 4 'Contact Person
                If ByteClick <> 1 Then .Cols(dsPrevRecord.Tables(0).Columns.Count).Width = .Width / 8 * 4 'Status

            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowPreview")
            'MyConnection.Close()
        End Try
    End Sub
#End Region

End Class